<?php

class Liga {
	
	public $id;
	public $ime;
	public $sezona;
	
	public $teams;
	public $rounds;
	
	// constructor
	
	public function __construct($i = 0) {
		$this->id = $i;
		
		if ($i != 0) {
			$n = $this->getfromdb($i);
			$this->sezona = $n['sezona'];
			$this->ime = $n['ime'];
		}
		// print "Pozvan je konstuktor za objekat Liga!";
	}
	
	// functions
	
	public function getfromdb($number) {
		$q = "SELECT id, ime, sezona FROM liga WHERE id = " . $number;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) throw new Exception("Ne postoji liga za zadati ID!");
		return (mysql_fetch_array($r));
	}
	
	public function getTeams() {
		$q = "SELECT tim FROM ucestvuje WHERE liga = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->roster[] = new Tim($n['tim']);
			}
		}
	}
	
	public function getRounds() {
		$q = "SELECT id FROM kolo WHERE liga = " . $this->id . " ORDER BY datum ASC";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->rounds[] = new Round($n['id']);
			}
		}
	}
	
	// admin
	
	public function insertIntoDb() {
		$q = "INSERT INTO liga (ime, sezona) VALUES ('".$this->ime."', '".$this->sezona."')";
		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			echo "Nova liga je dodata u bazu podataka";
		}
	}
} // end of class Liga


?>